<?php

namespace backend\controllers;

use backend\models\SignupForm;
use common\helpers\StringHelper;
use common\models\Task;
use common\models\WeworkUsers;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use Yii;
use common\models\User;
use backend\models\UserSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\web\Response;

/**
 * UserController implements the CRUD actions for User model.
 */
class UserController extends Controller
{
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class'   => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Finds the User model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return User the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = User::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    /**
     * @return string|Response
     * @throws NotFoundHttpException
     */
    public function actionPassword()
    {
        $model = $this->findModel(Yii::$app->user->identity->id);

        if ($model->load(Yii::$app->request->post())) {
            $model->password_hash = \Yii::$app->security->generatePasswordHash($model->password);
            $model->updated_at    = time();
            if ($model->save(false)) {
                Yii::$app->session->setFlash("success", "修改成功");
            } else {
                Yii::$app->session->setFlash("error", "修改失败:" . current($model->getFirstErrors()));
            }
            return $this->redirect("password");
        }

        return $this->render('password', [
            'model' => $model,
        ]);
    }

    /**
     * @return string|Response
     */
    public function actionCreate()
    {
        $model = new User();

        if ($model->load(Yii::$app->request->post())) {
            try {
                User::createAreaUser($model->username, $model->password, $model->email);
                Yii::$app->session->setFlash("success", "添加成功");
            } catch (\Exception $e) {
                Yii::$app->session->setFlash("error", $e->getMessage());
            }
            return $this->redirect("index");
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * @return string
     */
    public function actionIndex()
    {
        $searchModel = new UserSearch();
        $searchModel->is_area = 1;
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }
}
